<extend name="public/form"/>
<block name="css"></block>
<block name="content">
    <div class="layui-card-header "></div>
    <div class="layui-card-body ">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">父级导航栏</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="pid" id="pid-select" lay-verify="required" lay-filter="pid-select" lay-search>
                        <option value="0" data-level="1">顶级导航栏</option>
                        <volist name="$treeList" id="vo" empty="暂时没有数据">
                            <option value="{{$vo.id}}" data-level="{{$vo.level}}"
                            <eq name="vo.level" value="3">disabled</eq>
                            <if condition="$vo.id==$Request.get.id">selected</if>
                            >{{$vo._title}}</option>
                        </volist>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">导航栏名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请输入导航栏名称" placeholder="请输入导航栏名称"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">跳转链接</label>
                <div class="layui-input-block">
                    <input type="text" name="url" lay-reqtext="请输入跳转链接" placeholder="跳转链接"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="text" name="sorts" lay-verify="number|required" value="100" lay-reqtext="排序必须为数字"
                           placeholder="导航栏排序" autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="status" title="显示" value="1" checked/>
                    <input type="radio" name="status" title="隐藏" value="0"/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
            <input type="hidden" name="level" value="0"/>
        </form>
    </div>
</block>
<block name="js">
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer;
            //监听提交
            form.on('submit(add)', function (data) {
                let send_json = data.field;
                send_json.level = $("#pid-select option:selected").data('level');
                //发异步，把数据提交给php
                $.ajax({
                    url: '/admin/nav/add',
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("添加成功", {
                                icon: 1
                            }, function () {
                                // 获得frame索引
                                parent.location.reload();
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });

                return false;
            });
        });
    </script>
</block>